﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ks.Fs.Entity;
using System.IO;
using Ks.Fs.Core.Common;
using System.Security.AccessControl;

namespace Ks.Fs.Core.Services
{
    public class FolderService : BaseService
    {
        public static int CreateFolder(Folder objFolder)
        {
            string folderpath = "";
            if (objFolder.ParentId > 0)
            {
                Folder objParent = GetFolder(objFolder.ParentId);
                folderpath = Path.Combine(objParent.Path,objFolder.FolderName);
            }
            else
            {
                folderpath = objFolder.FolderName;
            }
            objFolder.Path = folderpath;

            //check exits directory 
            string fullpath = Path.Combine(Config.RootPath, folderpath);

            if (Directory.Exists(fullpath))
            {
                return GetFolder(folderpath).Id;
            }
            else
            {                                
                DirectoryInfo dir = Directory.CreateDirectory(fullpath);                
                if (dir != null)
                {
                    return DataProvider.SaveOrUpdateFolder(objFolder);
                }
            }            

            return -1;
        }

        public static bool ChangeName(int folderId, string newFolderName)
        {
            throw new NotImplementedException();                         
        }

        public static bool DeleteFolder(int folderId)
        {
            Folder objFolder = DataProvider.GetFolder(folderId);
            if (objFolder != null)
            {
                Directory.Delete(Path.Combine(Config.RootPath,objFolder.Path),true);
                return DataProvider.DeleteFolder(folderId);
            }
            return false;
        }

        public static Folder GetFolder(int FolderId)
        {
            return DataProvider.GetFolder(FolderId);
        }      

        public static List<Folder> GetFolders(int parentId, string owner,int sortBy,bool isSortAscending, int PageSize, int PageIndex)
        {
            return DataProvider.GetFolders(parentId,owner,sortBy,isSortAscending,PageSize,PageIndex);
        }

        public static Folder GetFolder(string path)
        {
            throw new NotImplementedException();
        }

    }
}
